package com.wanghai.carwashing.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 日期操作工具类
 * @author 王海
 */
public class DateUtil {

    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT = "yyyy-MM-dd";
	
    public static final String REGIST_TIME_FORMAT = YYYY_MM_DD_HH_MM_SS;

    public static final String LAST_LOGIN_TIME_FORMAT = YYYY_MM_DD_HH_MM_SS;

    public static final SimpleDateFormat DATE_DATEFORMAT = new SimpleDateFormat(DATE_FORMAT);
	
    public static final String [] DAY_OF_WEEKS = new String[]{"","周一","周二","周三","周四","周五","周六","周日"};

    /**
     * 获取某个日期对应的：年-月-日
     * @param date
     * @return 
     */
    public static final String getDay(Date date){
            return DATE_DATEFORMAT.format(date);
    }
    /**
     * 根据日期格式把日期转换成日期字符串
     * @param date
     * @param format
     * @return
     */
    public static final String formatTime(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format!=null?format:YYYY_MM_DD_HH_MM_SS);
        return sdf.format(date);
    }
	
    public static String format(Date date){
        DateFormat df = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return df.format(date);
    }

    public static Date parse(String date){
        DateFormat df1 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
                return df1.parse(date);
        } catch (ParseException e) {
                try {
                        return df2.parse(date);
                } catch (ParseException e1) {
                        return null;
                }
        }
    }
	
    /**
     * 根据日期解析出是星期几
     * @param date
     * @return
     */
    public static final String parseDayOfWeek(String date){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
                d  = df.parse(date);
        } catch (ParseException e) {
                e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        dayOfWeek =(dayOfWeek==7)?6:(dayOfWeek==1?7:dayOfWeek-1);
        return String.valueOf(dayOfWeek);
    }
        
    /**
     * 把日期转换成日期(星期x)的格式
     * @param day
     * @return
     */
    public static String formatDayOfWeek(String day) {
        StringBuffer sb = new StringBuffer();
        sb.append(day);
        sb.append("(");
        sb.append(DAY_OF_WEEKS[Integer.parseInt(DateUtil.parseDayOfWeek(day))]);
        sb.append(")");
        return sb.toString();
    }
	
    public static final String reFormatTime(String time,String dateFormat){
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            Date d1 = sdf.parse(time);
            result = sdf.format(d1);
        } catch (ParseException e) {
            throw new IllegalArgumentException("时间格式不正确");
        }
        return result;
    }
}
